package com.ncloud.resman.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.entity.Cabinet;
import com.ncloud.resman.service.impl.CabinetServiceImpl;

import java.io.IOException;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created by gaowei on 2017/8/11.
 */
@RestController
@RequestMapping("/resman/cabinet")
public class CabinetController {
	@Autowired
	private CabinetServiceImpl cabinetService;
    @GetMapping(value = "/list/{areaId}/{countryId}/{provinceId}/{cityId}/{roomId}/{placeNum}/{state}")
    public Result<?> getCabinetInfo(HttpServletRequest request,
    		@PathVariable("areaId") Integer areaId,@PathVariable("countryId") Integer countryId,
    		@PathVariable("provinceId") Integer provinceId,@PathVariable("cityId") Integer cityId,
    		@PathVariable("roomId") Integer roomId,@PathVariable("placeNum") Integer placeNum,
    		@PathVariable("state") Integer state){
    	JSONObject params = new JSONObject();
    	params.put("areaId", areaId);
    	params.put("countryId", countryId);
    	params.put("provinceId", provinceId);
    	params.put("cityId", cityId);
    	params.put("roomId", roomId);
    	params.put("placeNum", placeNum);
    	params.put("state", state);
    	JSONArray infos = cabinetService.getCabinetInfo(params);
		return ResultUtil.success(infos);
    }
    @PostMapping(value = "/add")
    public Result<?> add(HttpServletRequest request, @RequestBody Cabinet cabinet){
    	Object userID = request.getAttribute("userID");
    	cabinet.setCreateUser(userID==null?1:Long.parseLong(userID.toString()));
    	cabinet.setCreateDate(new Date());
    	int num = cabinetService.insertCabinet(cabinet);
		return ResultUtil.success(num);
    }
    @PutMapping(value = "/update")
    public Result<?> update(HttpServletRequest request, @RequestBody Cabinet cabinet){
    	Object userID = request.getAttribute("userID");
    	cabinet.setUpdateUser(userID==null?1:Long.parseLong(userID.toString()));
    	cabinet.setUpdateDate(new Date());
    	int num = cabinetService.updateCabinet(cabinet);
		return ResultUtil.success(num);
    }
    @DeleteMapping(value = "/delete/{cabinetId}")
    public Result<?> delete(HttpServletRequest request, @PathVariable("cabinetId") Long cabinetId){
    	int num = cabinetService.deleteCabinet(cabinetId);
		return ResultUtil.success(num);
    }
    @PostMapping(value = "/import")
    public Result<?> importCabinet(HttpServletRequest request, @RequestParam(value="file",required=false) MultipartFile file) throws IOException{
    	Object userID = request.getAttribute("userID");
    	Long userId = userID==null?1:Long.parseLong(userID.toString());
    	int num = cabinetService.importCabinet(userId,file.getInputStream());
		return ResultUtil.success(num);
    }
    @GetMapping(value = "/export")
    public Result<?> exportCabinet(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	JSONObject params = new JSONObject();
    	int exportNum=cabinetService.exportCabinet(params, response);
		return ResultUtil.success(exportNum);
    }
    @GetMapping(value = "/exporttemplate")
    public Result<?> exportTemplate(HttpServletRequest request,HttpServletResponse response) throws IOException{
    	int fileNum=cabinetService.exportTemplate(response);
		return ResultUtil.success(fileNum);
    }
}
